import '@ant-design/compatible/assets/index.css';
import React, { useState, useEffect } from 'react';
import { PageHeaderWrapper } from "@ant-design/pro-layout";
import {message, Spin} from 'antd';
import DataForm from './components/DataForm';
import {queryRule} from './service';

import * as BdHelper from '@/utils/BdHelper';
import * as DataHelper from '@/helpers/DataHelper';
import * as TsxHelper from '@/helpers/TsxHelper';

export default (props: any)=>{
  // common数据
  const [common, setCommon] = useState({});
  // 表单数据
  const [dataFormValues, setDataFormValues] = useState<any>();
  // 初始化loading
  const [initLoading, setInitLoading] = useState(true);
  const [obj,setObj] = useState<any>({})

  const { location } = props;

  const isUpdate: boolean = true;
  
 console.log(isUpdate)
  useEffect(() => {
    //请求当前一组配置参数，tag=sites
    queryRule({tag:'sites'}).then(res=>{
      if(res.code == 0){
        setCommon(res.data?.common);
        if(res.data.list && res.data.list.length>0){
          setDataFormValues(res.data.list[0]?.item_value_decode);
          var list = res.data.list[0]?.item_value_decode;
          var valueObj = {};
          if(list && Array.isArray(list) && list.length>0){
            list.map(item=>{
              valueObj[item.alias_key] = item.item_value
            })
          }
          setObj(valueObj);
          console.log(valueObj);
        }
      }else{
        message.error(res.message);
      }
      setInitLoading(false);  //这个要放在解析数据之后，或者有可能导致页面渲染部分组件为空数据
    });
  }, []);


  return <PageHeaderWrapper>
    <Spin spinning={initLoading}>
      {initLoading && <div style={{height: '500px',backgroundColor: '#fff'}} />}
      {!initLoading && <DataForm 
                          isUpdate={isUpdate}
                          obj={obj}
                          dataFormValues={dataFormValues}
                          common={common}/>}
    </Spin>
  </PageHeaderWrapper>
};
